package com.meixiang.activity.account.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 *
 * @author wzz
 */

public class ForgetPasswordActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView mTitle;
    @Bind(R.id.cel_input_verify_phone_number)
    ClearEditText mCelInputVerifyPhoneNumber;
    @Bind(R.id.tv_confirm)
    CheckEditTextEmptyButton mTvConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        mTitle.setTitle(R.string.verfy_phone_name);
        mTitle.setLeftImageButton(R.mipmap.back);
        mTitle.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    @Override
    protected void initView() {
        mTvConfirm.setEditText(mCelInputVerifyPhoneNumber);
    }

    @Override
    protected void setData() {

    }


    @Override
    @OnClick(R.id.tv_confirm)
    public void onClick(View v) {
        if (mCelInputVerifyPhoneNumber.getText().toString().trim().length() < 11) {
            AbToastUtil.showToast(context, "请输入手机号码");
            return;
        }
        sendPhone();
    }


    private void sendPhone() {

        HttpParams params = new HttpParams();
        params.put("mobile", mCelInputVerifyPhoneNumber.getText().toString());
        HttpUtils.post(Config.SERVER_URL + "getReSetPwdCode", params, new HttpCallBack(ForgetPasswordActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (code.equals("0")) {
                    Intent intent = new Intent(ForgetPasswordActivity.this, ReviseLoginPasswordActivity.class);
                    intent.putExtra("phone", mCelInputVerifyPhoneNumber.getText().toString());
                    startActivity(intent);
                    finish();
                } else {
                    mCelInputVerifyPhoneNumber.setText("");
                    AbToastUtil.showToast(context, message);
                }


            }

            @Override
            public void onError(String code, String message) {
                mCelInputVerifyPhoneNumber.setText("");
                AbToastUtil.showToast(context, message);
            }

        });
    }

}
